﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using OpenGado.DATA.Entities;
using OpenGado.DATA;
using OpenGado.Business;

namespace OpenGado.Win
{
    public partial class frmCategoryManagement : Form
    {
        public frmCategoryManagement()
        {
            InitializeComponent();
            GetCategories();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            // will add a new category
            frmCategory categoryForm = new frmCategory();

            categoryForm.CategoriesUpdated += new frmCategory.UpdateCategoriesHandler(GetCategories);
            //categoryForm.Parent = this;
            categoryForm.Show();

        }

        private void GetCategories()
        {
            dgvCategories.AutoGenerateColumns = false;
            IList<Categories> categoryList = new List<Categories>();

            categoryList = CategoryDataControl.Instance.GetAllCategories();
            
            // perform manual binding.

            dgvCategories.DataSource = categoryList;
            dgvCategories.Columns[0].DataPropertyName = "CategoryId";
            dgvCategories.Columns[1].DataPropertyName = "CategoryName";

            
           
       
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            // TODO: Perform  a business check prior to deleting a category that its empty
            DialogResult res;

            res = MessageBox.Show("Are you sure  you want to delete the selected categories? ", "Inventory", MessageBoxButtons.YesNo);
            
            if (res == DialogResult.Yes)
            {
                foreach (int i in SelectedCategories())
                   {
                       Categories ct = new Categories();
                       ct.CategoryId = i;
                       CategoryDataControl.Instance.Delete(ct);
                   }

            }


        }

        private List<int> SelectedCategories()
        {
            List<int> selectedCategories = new List<int>();
            int categoryId;

            foreach (DataGridViewRow rw in dgvCategories.SelectedRows)
            {
                int.TryParse(rw.Cells[0].Value.ToString(), out categoryId);

                if (!selectedCategories.Contains(categoryId))
                {
                    selectedCategories.Add(categoryId);
                }
            }

            return selectedCategories;
        }

        private void dgvCategories_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            int categoryId;

            int.TryParse(dgvCategories.Rows[e.RowIndex].Cells[0].Value.ToString(), out categoryId);

            frmCategory catfrm = new frmCategory(categoryId);
            catfrm.CategoriesUpdated += new frmCategory.UpdateCategoriesHandler(GetCategories);
            catfrm.Show();
        }

       
       
    }
}
